
package com.huaweicloud.haydn.delivercore.agent.common;

import java.util.Locale;

import org.apache.commons.lang3.StringUtils;

import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessErrorEnum;
import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessException;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 功能描述
 *
 * @since 2023-03-17
 */

@Getter
@AllArgsConstructor
public enum DbTypeEnum {
    DWS("DWS", "dws"),
    MRS_HUDI("MRS-HUDI", "MRS-Hudi"),
    ORACLE("ORACLE", "oracle"),
    MYSQL("MYSQL", "mysql");

    // 配置文件中对应的数据库类型
    private String configDbType;

    // 对应的各个数据库中的实例获取的数据库类型
    private String databaseUtilComponent;

    // 根据用户传入配置文件中数据库类型获取到对应的数据库示例的名称，用于获取对应的DatabaseUtil实现类
    public static String getDatabaseUtilComponentByConfigType(String importConfigDbType) {
        try {
            DbTypeEnum dbTypeEnum = getValueByName(importConfigDbType);
            return dbTypeEnum.getDatabaseUtilComponent();
        } catch (IllegalArgumentException e) {
            throw new BusinessException(BusinessErrorEnum.BASE_BAD_REQUEST, "invalid database type!");
        }
    }

    public static DbTypeEnum getValueByName(String dbType) {
        dbType = StringUtils.defaultString(dbType).toUpperCase(Locale.ROOT);
        for (DbTypeEnum dbTypeEnum : DbTypeEnum
            .values()) {
            if (dbTypeEnum.getConfigDbType().equals(dbType)) {
                return dbTypeEnum;
            }
        }
        throw new BusinessException(BusinessErrorEnum.BASE_BAD_REQUEST, "invalid database type!");
    }
}
